package com.packet.cheng.controller;

import com.packet.cheng.annotation.LogAnnotation;
import com.packet.cheng.dto.RedPacketParamDTO;
import com.packet.cheng.entity.RedPacketRecord;
import com.packet.cheng.service.RedPacketService;
import com.packet.cheng.service.UserService;
import com.packet.cheng.util.R;
import com.packet.cheng.vo.RedPacketReocrdVo;
import com.packet.cheng.vo.RedPacketVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 张在成
 */
@RestController
@RequestMapping("/redPacket")
public class RedPacketController{

    @Resource
    private RedPacketService redPacketService;

    /**
     * 发红包
     * @return
     */
    @LogAnnotation("发红包")
    @PostMapping("/send")
    public R<?> sendRedPacket(@RequestHeader("token") String token, @RequestBody RedPacketParamDTO redPacketParamDTO) throws Exception {
        return redPacketService.insertRedPacket(redPacketParamDTO,token);
    }

    /**
     * 抢红包
     * @return
     */
    @PostMapping("/getRedPacket")
    public R<?> robRedPacket(@RequestHeader("token") String token,@RequestParam("id") Integer id) throws Exception {
        R<RedPacketVo> packetVo = redPacketService.grabRedPacket(token, id);
        return  packetVo;
    }

    /**
     * 拆红包
     * @return
     */
    @PostMapping("/apartRedPacket")
    public R<?> apartRedPacket(@RequestHeader("token") String token,@RequestParam("id") Integer id) throws Exception {
        R<RedPacketReocrdVo> packetReocrdVoR = redPacketService.apartRedPacket(token, id);
        return  packetReocrdVoR;
    }

    /**
     * 查询指定红包的领取记录
     * @return
     */
    @GetMapping("/getRedPacketRecord/{id}")
    public R<?> getRedPacket(@RequestHeader("token") String token,@PathVariable("id") Integer id) throws Exception {
        R<List<RedPacketRecord>> redPacket = redPacketService.selectRedPacket(token, id);
        return  redPacket;
    }



    /**
     * 红包过期退款，通过定时任务
     * @return
     */
    @GetMapping("/refund")
    public R<?> refundRedPacket(){

        return  null;
    }

}
